<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小账单物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82882996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\property\work;

use think\App;
use app\controller\property\Base;
use app\validate\work\WorkExpendApplyValidate;
use app\common\repositories\work\WorkCateRepository;
use app\common\repositories\work\WorkExpendApplyRepository;
use app\common\repositories\approval\ApprovalConfigRepository;

class WorkExpendApply extends Base
{

    protected $repository;

    public function __construct(App $app, WorkExpendApplyRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;
        $this->assign([
            'addAuth' => property_auth('propertyWorkExpendApplyAdd'),
        ]);
    }

    /**
     * 报销申请
     * @return string|\think\response\Json|\think\response\View
     * @throws \Exception
     *
     * @author Amos
     * 2024-05-23 20:56
     */
    public function expendApplyAdd()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'config_id' => '',
                'cate_id' => '',
                'amount' => '',
                'picture' => [],
                'use_time' => '',
                'remark' => '',
            ]);
            $param['user_id'] = $this->request->userId;
            validate(WorkExpendApplyValidate::class)->scene('add')->check($param);
            $res = $this->repository->addInfo($param, $this->request->propertyId, $this->request->userId);
            if ($res) {
                return $this->success('申请成功');
            } else {
                return $this->error('申请失败');
            }
        } else {
            /** @var ApprovalConfigRepository $approvalConfigRepository */
            $approvalConfigRepository = app()->make(ApprovalConfigRepository::class);
            /** @var WorkCateRepository $workCateRepository */
            $workCateRepository = app()->make(WorkCateRepository::class);
            $cateData = $workCateRepository->getCateData($this->request->propertyId, ['is_type' => 1]);
            $configData = $approvalConfigRepository->getConfigData($this->request->propertyId, ['is_show' => 1, 'is_type' => 2, 'flow_type' => 'expend_apply']);
            return $this->fetch('work/expend/submit_add', [
                'cateData' => $cateData,
                'configData' => $configData
            ]);
        }
    }

    public function expendApplyEdit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'id' => '',
                'amount' => '',
                'use_time' => '',
                'picture' => [],
                'remark' => '',
            ]);
            $param['picture'] = implode(',', (array)$param['picture']);
            validate(WorkExpendApplyValidate::class)->scene('edit')->check($param);
            $info = $this->repository->get($param['id']);
            if (!$info) {
                return $this->error('信息错误');
            }
            try {
                $res = $this->repository->editInfo($info, $param, $this->request->userId);
                if ($res) {
                    return $this->success('修改成功');
                } else {
                    return $this->error('修改失败');
                }
            } catch (\Exception $e) {
                exception_log('修改密码失败', $e);
                return $this->error('网络错误');
            }
        } else {
            return $this->fetch('property/users/edit_password');
        }
    }


}